<?php
/**
 * Export en PDF des réservations
 *
 * @package zfbook
 * @subpackage controller
 */
class Zfbook_Controller_ActionHelpers_ExportReservations_Pdf
extends Zfbook_Controller_ActionHelpers_ExportReservations_Abstract 
{
    const MIME_TYPE = 'application/pdf';

    /**
     * Type mime PDF
     *
     * @return string
     */
    public function getMimeType()
    {
        return self::MIME_TYPE;
    }

    /**
     * Construit le PDF contenant la liste des réservations
     *
     * @return string
     */
    public function getContent()
    {
        // Création d'un nouveau document PDF
        $pdf = new Zend_Pdf();
        $pdf->setMemoryManager($memory = new Zend_Memory_Manager(Zfbook_Cache::getCacheInstance()->getBackend()));
        $memory->setMemoryLimit(200000);
        
        // Style par défaut du document
        $style = new Zend_Pdf_Style();
        $lineColor = new Zend_Pdf_Color_GrayScale(0.2);
        $style->setLineColor($lineColor);
        $style->setLineWidth(1);
        $helvetica = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $style->setFont($helvetica, 18);

        // Page d'accueil
        $pdf->pages[] = ($homePage = $pdf->newPage('A4'));
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
        $homePage->setFont($font, 36);
        $homePage->drawText('Liste des réservations', 120, 500);
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $homePage->setFont($font, 14);
        $dateNow = new Zend_Date();
        $homePage->drawText('Export du ' . $dateNow , 200, 450);
        $homePage->drawRectangle(20, 20, $homePage->getWidth() - 20, $homePage->getHeight() - 20, 0);

        $imagePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'header.jpg';
        $imageHeader = Zend_Pdf_Image::imageWithPath($imagePath);
        foreach ($this->_resaTab as $item) {
            $pdf->pages[] = ($page = $pdf->newPage('A4'));
            $page->drawImage($imageHeader, 20, 
                             $homePage->getHeight() - 220, 
                             $homePage->getWidth()  - 20, 
                             $homePage->getHeight() - 20);
            $page->drawRectangle(20, 20, 
                             $homePage->getWidth()  - 20, 
                             $homePage->getHeight() - 20, 0);
            $page->setStyle($style);
            $y  = 20;
            $x1 = 40;
            $x2 = 180;
            $page->drawText('Salle: ',           $x1, $y * 30);
            $page->drawText($item['room'],       $x2, $y-- * 30);
            $page->drawText('Usage: ',           $x1, $y * 30);
            $page->drawText($item['usage'],      $x2, $y-- * 30);
            $page->drawText('Date de début: ',   $x1, $y * 30);
            $page->drawText($item['date_begin'], $x2, $y-- * 30);
            $page->drawText('Date de fin: ',     $x1, $y * 30);
            $page->drawText($item['date_end'],   $x2, $y-- * 30);
            $page->drawText('Créateur: ',        $x1, $y * 30);
            $page->drawText($item['user'],       $x2, $y-- * 30);
        }
        
        // Rendu du contenu PDF
        return $pdf->render();
    }
}
